package cn.iocoder.yudao.module.picturebook.service.bookgather;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.bookgather.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.bookgather.BookGatherDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.picturebook.dal.mysql.bookgather.BookGatherMapper;


/**
 * 绘本集合 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class BookGatherServiceImpl implements BookGatherService {

    @Resource
    private BookGatherMapper bookGatherMapper;

    @Override
    public Integer createBookGather(BookGatherSaveReqVO createReqVO) {
        // 参数校验
        if (createReqVO == null) {
            return null;
        }

        // 插入
        BookGatherDO bookGather = BeanUtils.toBean(createReqVO, BookGatherDO.class);
        bookGatherMapper.insert(bookGather);

        // 添加空值检查
        if (bookGather.getGatherId() == null) {
            return null;
        }

        // 返回
        return bookGather.getGatherId();
    }

    @Override
    public void updateBookGather(BookGatherSaveReqVO updateReqVO) {
        // 参数校验
        if (updateReqVO == null) {
            return;
        }
        if (updateReqVO.getGatherId() == null) {
            return;
        }

        // 校验存在
        if (bookGatherMapper.selectById(updateReqVO.getGatherId()) == null) {
            return;
        }
        // 更新
        BookGatherDO updateObj = BeanUtils.toBean(updateReqVO, BookGatherDO.class);
        bookGatherMapper.updateById(updateObj);
    }

    @Override
    public void deleteBookGather(Integer id) {
        // 参数校验
        if (id == null) {
            return;
        }

        // 校验存在
        if (bookGatherMapper.selectById(id) == null) {
            log.error("[deleteBookGather][id({}) 不存在]", id);
            return;
        }
        // 删除
        bookGatherMapper.deleteById(id);
    }

    @Override
    public void deleteBookGatherListByIds(List<Integer> ids) {
        // 参数校验
        if (ids == null || ids.isEmpty()) {
            return;
        }

        // 删除
        bookGatherMapper.deleteByIds(ids);
    }


    private void validateBookGatherExists(Integer id) {
        // 参数校验
        if (id == null) {
            return;
        }

        if (bookGatherMapper.selectById(id) == null) {
            log.error("[getBookGather][id({}) 不存在]", id);
            // 不抛出异常，仅记录日志
            return;
        }
    }

    @Override
    public BookGatherDO getBookGather(Integer id) {
        // 参数校验
        if (id == null) {
            return null;
        }

        return bookGatherMapper.selectById(id);
    }

    @Override
    public PageResult<BookGatherDO> getBookGatherPage(BookGatherPageReqVO pageReqVO) {
        // 参数校验
        if (pageReqVO == null) {
            return PageResult.empty();
        }

        return bookGatherMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BookGatherDO> getBookGatherByCategoryId(Integer categoryId) {
        // 参数校验
        if (categoryId == null) {
            return Collections.emptyList();
        }

        return bookGatherMapper.selectListByCategoryId(categoryId);
    }

}